<?php	

/**
 * students_for_change functions and definitions
 *
 * For more information on hooks, actions, and filters, @link http://codex.wordpress.org/Plugin_API
 */

/*
 * Set up the content width value based on the theme's design.
 *
 */

if ( ! isset( $content_width ) )
	$content_width = 630;



//Load Other CSS files

function students_for_change_other_css() { 
if ( !is_admin() ) {	
wp_enqueue_style( 'students_for_change_other', get_template_directory_uri() . '/css/1140.css' );


}  }
add_action('wp_enqueue_scripts', 'students_for_change_other_css');	

function students_for_change_other1_css() { 
if ( !is_admin() ) {	


}  }
add_action('wp_enqueue_scripts', 'students_for_change_other1_css');	

function students_for_change_other2_css() { 
if ( !is_admin() ) {	


}  }



 
 



function students_for_change_fonts_css() { 
if ( !is_admin(
) ) {
	}
}
add_action('wp_enqueue_scripts', 'students_for_change_fonts_css');	

//Load Custom CSS
function students_for_change_customstyle() { ?>
<?php if(of_get_option('sldrtxt_checkbox') == "0"){ ?>
<?php } ?>


<?php if(of_get_option('sldrtitle_checkbox') == "0"){ ?>
<?php } ?>

<?php if(of_get_option('sldrdes_checkbox') == "0"){ ?>
<?php } ?>


<?php }

add_action( 'wp_head', 'students_for_change_customstyle' );



//Load Java Scripts to header
function students_for_change_head_js() { 
	if ( !is_admin() ) {
		wp_enqueue_script('jquery');
		
		
		
		if(of_get_option('disslight_checkbox') == "0")
		if ( is_singular() ) wp_enqueue_script( 'comment-reply' );
	}
}
add_action('wp_enqueue_scripts', 'students_for_change_head_js');



//Load Java Scripts to Footer
add_action('wp_footer', 'students_for_change_load_js');

function students_for_change_load_js() {

} 



 


/* students_for_change welcome text */








//students_for_change get the first image of the post Function
function students_for_change_get_images($overrides = '', $exclude_thumbnail = false)
{
    return get_posts(wp_parse_args($overrides, array(
        'numberposts' => -1,
        'post_parent' => get_the_ID(),
        'post_type' => 'attachment',
        'post_mime_type' => 'image',
        'order' => 'ASC',
        'exclude' => $exclude_thumbnail ? array(get_post_thumbnail_id()) : array(),
        'orderby' => 'menu_order ID'
    )));
}




//ADD FULL WIDTH BODY CLASS
add_filter( 'body_class', 'students_for_change_fullwdth_body_class');
function students_for_change_fullwdth_body_class( $classes ) {
     if(of_get_option('nosidebar_checkbox') == "1")
          $classes[] = 'students_for_change_fullwdth_body';
     return $classes;
}

//Custom Excerpt Length
function students_for_change_excerptlength_teaser($length) {
    return 30;
}
function students_for_change_excerptlength_index($length) {
    return 12;
}
function students_for_change_excerptmore($more) {
    return '...';
}

function students_for_change_excerpt($length_callback='', $more_callback='') {
    global $post;
    if(function_exists($length_callback)){
        add_filter('excerpt_length', $length_callback);
    }
    if(function_exists($more_callback)){
        add_filter('excerpt_more', $more_callback);
    }
    $output = get_the_excerpt();
    $output = apply_filters('wptexturize', $output);
    $output = apply_filters('convert_chars', $output);
    $output = '<p>'.$output.'</p>';
    echo $output;
}


/*-----------------------------------------------------------------------------------*/
/* Display <title> tag
/*-----------------------------------------------------------------------------------*/

// filter function for wp_title
function students_for_change_filter_wp_title( $old_title, $sep, $sep_location ){
 
    // add padding to the sep
    $ssep = ' ' . $sep . ' ';
     
    // find the type of index page this is
    if( is_category() ) $insert = $ssep . __('Category','students_for_change');
    elseif( is_tag() ) $insert = $ssep . __('Tag','students_for_change');
    elseif( is_author() ) $insert = $ssep . __('Author','students_for_change');
    elseif( is_year() || is_month() || is_day() ) $insert = $ssep . __('Archives','students_for_change');
    elseif( is_home() ) $insert = $ssep . get_bloginfo('description');
    else $insert = NULL;
     
    // get the page number we're on (index)
    if( get_query_var( 'paged' ) )
    $num = $ssep . __('Page ','students_for_change') . get_query_var( 'paged' );
     
    // get the page number we're on (multipage post)
    elseif( get_query_var( 'page' ) )
    $num = $ssep . __('Page ','students_for_change') . get_query_var( 'page' );
     
    // else
    else $num = NULL;

     
    // concoct and return new title
    return get_bloginfo( 'name' ) . $insert . $old_title . $num;
}

add_filter( 'wp_title', 'students_for_change_filter_wp_title', 10, 3 );

function students_for_change_rss_title($title) {
    /* need to fix our add a | blog name to wp_title */
    $ft = str_replace(' | ','',$title);
    return str_replace(get_bloginfo('name'),'',$ft);
}
add_filter('get_wp_title_rss', 'students_for_change_rss_title',10,1);

	







//SIDEBAR
function students_for_change_widgets_init(){
	register_sidebar(array(
	'name'          => __('Right Sidebar', 'students_for_change'),
	'id'            => 'sidebar',
	'description'   => __('Right Sidebar', 'students_for_change'),
	'before_widget' => '<div id="%1$s" class="widget %2$s"><div class="widget_wrap">',
	'after_widget'  => '</div></div>',
	'before_title'  => '<h3 class="widgettitle">',
	'after_title'   => '</h3>'
	));
	
	register_sidebar(array(
	'name'          => __('Footer Widgets', 'students_for_change'),
	'id'            => 'foot_sidebar',
	'description'   => __('Widget Area for the Footer', 'students_for_change'),
	'before_widget' => '<div id="%1$s" class="widget %2$s"><div class="widget_wrap">',
	'after_widget'  => '</div></div>',
	'before_title'  => '<h3 class="widgettitle">',
	'after_title'   => '</h3>'
	));



 
	
	
}

add_action( 'widgets_init', 'students_for_change_widgets_init' );








//**************students_for_change SETUP******************//
function students_for_change_setup() {
//Custom Background
add_theme_support( 'custom-background', array(
	'default-color' => '',
	'default-image' => get_template_directory_uri() . ''
) );

add_theme_support('automatic-feed-links');

//Post Thumbnail	
   add_theme_support( 'post-thumbnails' );
   
   
//Register Menus
	register_nav_menus( array(
		'primary' => __( 'Primary Navigation(Header)', 'students_for_change' ),
		
	) );

 // Enables post and comment RSS feed links to head
    add_theme_support('automatic-feed-links');


// Make theme available for translation
 load_theme_textdomain('asteria', get_template_directory() . '/languages');  
	
	
        
    
/*
 * Loads the Options Panel
 *
 * If you're loading from a child theme use stylesheet_directory
 * instead of template_directory
 */

define( 'OPTIONS_FRAMEWORK_DIRECTORY', get_template_directory_uri() . '/admin/' );
require_once dirname( __FILE__ ) . '/admin/options-framework.php';

include(get_template_directory() . '/lib/widgets.php');


}
add_action( 'after_setup_theme', 'students_for_change_setup' );


















/* ******************************************************** */
/* ******************************************************** */
/*															*/
/*				Students For Change Functions				*/
/*															*/
/* ******************************************************** */
/* ******************************************************** */


/*
 * Function for debuging.
 * 
 * This is used to print a formatted array on screen.
 */
function sfc_print_array($array) {
	echo '<pre>'.print_r($array, true).'</pre>';
}




/*
 * get the first image of the post Function
 */
function sfc_get_featured_posts()
{
	$args = array(
		'post_type'		=> 'post',
		'post_status'	=> 'publish',
		'meta_query' 	=> array( array('key' => 'meta_featured', 'value' => 'on') )
	);
	return get_posts($args);
}

function sfc_get_featured_posts()
{
	$args = array(
		'post_type'		=> 'post',
		'post_status'	=> 'publish',
		'template'			=> 'Game 1'
	);
	return get_posts($args);
}




/*
 * Adds additional meta boxes on the posts pages
 */
function cd_meta_box_cb()
{
	global $post;
	$values = get_post_custom( $post->ID );
	
	$team_name = isset( $values['meta_team_name'] ) ? esc_attr( $values['meta_team_name'][0] ) : '';
	$summary = isset( $values['meta_summary'] ) ? esc_attr( $values['meta_summary'][0] ) : '';
	$topic = isset( $values['meta_topic'] ) ? esc_attr( $values['meta_topic'][0] ) : '';
	$video = isset( $values['meta_video'] ) ? esc_attr( $values['meta_video'][0] ) : '';
	$exp = isset( $values['meta_expert'] ) ? esc_attr( $values['meta_expert'][0] ) : '';
	$call = isset( $values['meta_call'] ) ? esc_attr( $values['meta_call'][0] ) : '';
	$blog = isset( $values['meta_blog'] ) ? esc_attr( $values['meta_blog'][0] ) : '';
	$featured = isset( $values['meta_featured'] ) ? esc_attr( $values['meta_featured'][0] ) : '';
	
	$quiz1_question = isset( $values['meta_quiz1_question'] ) ? esc_attr( $values['meta_quiz1_question'][0] ) : '';
	$quiz1_correct = isset( $values['meta_quiz1_correct'] ) ? esc_attr( $values['meta_quiz1_correct'][0] ) : '';
	$quiz1_wrong1 = isset( $values['meta_quiz1_wrong1'] ) ? esc_attr( $values['meta_quiz1_wrong1'][0] ) : '';
	$quiz1_wrong2 = isset( $values['meta_quiz1_wrong2'] ) ? esc_attr( $values['meta_quiz1_wrong2'][0] ) : '';
	$quiz1_wrong3 = isset( $values['meta_quiz1_wrong3'] ) ? esc_attr( $values['meta_quiz1_wrong3'][0] ) : '';
	
	$quiz2_question = isset( $values['meta_quiz2_question'] ) ? esc_attr( $values['meta_quiz2_question'][0] ) : '';
	$quiz2_correct = isset( $values['meta_quiz2_correct'] ) ? esc_attr( $values['meta_quiz2_correct'][0] ) : '';
	$quiz2_wrong1 = isset( $values['meta_quiz2_wrong1'] ) ? esc_attr( $values['meta_quiz2_wrong1'][0] ) : '';
	$quiz2_wrong2 = isset( $values['meta_quiz2_wrong2'] ) ? esc_attr( $values['meta_quiz2_wrong2'][0] ) : '';
	$quiz2_wrong3 = isset( $values['meta_quiz2_wrong3'] ) ? esc_attr( $values['meta_quiz2_wrong3'][0] ) : '';
	
	$quiz3_question = isset( $values['meta_quiz3_question'] ) ? esc_attr( $values['meta_quiz3_question'][0] ) : '';
	$quiz3_correct = isset( $values['meta_quiz3_correct'] ) ? esc_attr( $values['meta_quiz3_correct'][0] ) : '';
	$quiz3_wrong1 = isset( $values['meta_quiz3_wrong1'] ) ? esc_attr( $values['meta_quiz3_wrong1'][0] ) : '';
	$quiz3_wrong2 = isset( $values['meta_quiz3_wrong2'] ) ? esc_attr( $values['meta_quiz3_wrong2'][0] ) : '';
	$quiz3_wrong3 = isset( $values['meta_quiz3_wrong3'] ) ? esc_attr( $values['meta_quiz3_wrong3'][0] ) : '';
	
	$quiz4_question = isset( $values['meta_quiz4_question'] ) ? esc_attr( $values['meta_quiz4_question'][0] ) : '';
	$quiz4_correct = isset( $values['meta_quiz4_correct'] ) ? esc_attr( $values['meta_quiz4_correct'][0] ) : '';
	$quiz4_wrong1 = isset( $values['meta_quiz4_wrong1'] ) ? esc_attr( $values['meta_quiz4_wrong1'][0] ) : '';
	$quiz4_wrong2 = isset( $values['meta_quiz4_wrong2'] ) ? esc_attr( $values['meta_quiz4_wrong2'][0] ) : '';
	$quiz4_wrong3 = isset( $values['meta_quiz4_wrong3'] ) ? esc_attr( $values['meta_quiz4_wrong3'][0] ) : '';
	
	$quiz5_question = isset( $values['meta_quiz5_question'] ) ? esc_attr( $values['meta_quiz5_question'][0] ) : '';
	$quiz5_correct = isset( $values['meta_quiz5_correct'] ) ? esc_attr( $values['meta_quiz5_correct'][0] ) : '';
	$quiz5_wrong1 = isset( $values['meta_quiz5_wrong1'] ) ? esc_attr( $values['meta_quiz5_wrong1'][0] ) : '';
	$quiz5_wrong2 = isset( $values['meta_quiz5_wrong2'] ) ? esc_attr( $values['meta_quiz5_wrong2'][0] ) : '';
	$quiz5_wrong3 = isset( $values['meta_quiz5_wrong3'] ) ? esc_attr( $values['meta_quiz5_wrong3'][0] ) : '';
	
	// We'll use this nonce field later on when saving.
	wp_nonce_field( 'my_meta_box_nonce', 'meta_box_nonce' );
	?>
	<style>
		.meta_box {
			overflow: hidden;
		}
		.field-wrapper {
			width: 100%;
			margin: 10px 0;
		}
		.field-wrapper label {
			font-weight: bold;
		}
		.field-wrapper input, .field-wrapper textarea, .field-wrapper select {
			width: 98%;
		}
		.field-wrapper textarea {
			height: 75px;
		}
			.field-wrapper textarea.large { height: 150px; }
		.field-wrapper input[type=checkbox] {
			width: auto;
		}
		.spacer { padding-top: 10px; }
	</style>
	<div class="meta_box">
		<h2>Project Information</h2>
		
		<div class="field-wrapper">
			<span>A featured post is one of the projects that appear on the front page.</span><br/>
			<input type="checkbox" name="meta_featured" id="meta_featured" <?php checked($featured, 'on' ); ?> />
			<label for="meta_featured">Featured</label>
		</div>
		<div class="field-wrapper">
			<label for="meta_team_name">Team Name</label>
			<input type="text" name="meta_team_name" id="meta_team_name" value="<?php echo $team_name; ?>" />
		</div>
		<div class="field-wrapper">
			<label for="meta_summary">Project Summary</label>
			<textarea name="meta_summary" id="meta_summary" class="large"><?php echo $summary; ?></textarea>
		</div>
		<div class="field-wrapper">
			<label for="meta_topic">Project Topic</label>
			<input type="text" name="meta_topic" id="meta_topic" value="<?php echo $topic; ?>" />
		</div>
		<div class="field-wrapper">
			<label for="meta_video">Student Video URL</label>
			<input type="text" name="meta_video" id="meta_video" value="<?php echo $video; ?>" />
		</div>
		<div class="field-wrapper">
			<label for="meta_expert">Expert Video URL</label>
			<input type="text" name="meta_expert" id="meta_expert" value="<?php echo $exp; ?>" />
		</div>
		<div class="field-wrapper">
			<label for="meta_call">Call to Action</label>
			<textarea name="meta_call" id="meta_call"><?php echo $call; ?></textarea>
		</div>
		<div class="field-wrapper">
			<label for="meta_blog">Blog URL</label>
			<input type="text" name="meta_blog" id="meta_blog" value="<?php echo $blog; ?>" />
		</div>
		
		
		<hr/>
		
		
		<h2>Quiz Information</h2>
		
		<div class="field-wrapper">
			<label for="meta_quiz1_question">Quiz Question #1</label>
			<textarea name="meta_quiz1_question" id="meta_quiz1_question"><?php echo $quiz1_question; ?></textarea>
		</div>
		<div class="field-wrapper">
			<label for="meta_quiz1_correct">Correct Answer</label>
			<input type="text" name="meta_quiz1_correct" id="meta_quiz1_correct" value="<?php echo $quiz1_correct; ?>" />
		</div>
		<div class="field-wrapper">
			<label for="meta_quiz1_wrong1">Wrong Answer #1</label>
			<input type="text" name="meta_quiz1_wrong1" id="meta_quiz1_wrong1" value="<?php echo $quiz1_wrong1; ?>" />
		</div>
		<div class="field-wrapper">
			<label for="meta_quiz1_wrong2">Wrong Answer #2</label>
			<input type="text" name="meta_quiz1_wrong2" id="meta_quiz1_wrong2" value="<?php echo $quiz1_wrong2; ?>" />
		</div>
		<div class="field-wrapper">
			<label for="meta_quiz1_wrong3">Wrong Answer #3</label>
			<input type="text" name="meta_quiz1_wrong3" id="meta_quiz1_wrong3" value="<?php echo $quiz1_wrong3; ?>" />
		</div>
		
		<div class="spacer"></div>
		
		<div class="field-wrapper">
			<label for="meta_quiz2_question">Quiz Question #2</label>
			<textarea name="meta_quiz2_question" id="meta_quiz2_question"><?php echo $quiz2_question; ?></textarea>
		</div>
		<div class="field-wrapper">
			<label for="meta_quiz2_correct">Correct Answer</label>
			<input type="text" name="meta_quiz2_correct" id="meta_quiz2_correct" value="<?php echo $quiz2_correct; ?>" />
		</div>
		<div class="field-wrapper">
			<label for="meta_quiz2_wrong1">Wrong Answer #1</label>
			<input type="text" name="meta_quiz2_wrong1" id="meta_quiz2_wrong1" value="<?php echo $quiz2_wrong1; ?>" />
		</div>
		<div class="field-wrapper">
			<label for="meta_quiz2_wrong2">Wrong Answer #2</label>
			<input type="text" name="meta_quiz2_wrong2" id="meta_quiz2_wrong2" value="<?php echo $quiz2_wrong2; ?>" />
		</div>
		<div class="field-wrapper">
			<label for="meta_quiz2_wrong3">Wrong Answer #3</label>
			<input type="text" name="meta_quiz2_wrong3" id="meta_quiz2_wrong3" value="<?php echo $quiz2_wrong3; ?>" />
		</div>
		
		<div class="spacer"></div>
		
		<div class="field-wrapper">
			<label for="meta_quiz3_question">Quiz Question #3</label>
			<textarea name="meta_quiz3_question" id="meta_quiz3_question"><?php echo $quiz3_question; ?></textarea>
		</div>
		<div class="field-wrapper">
			<label for="meta_quiz3_correct">Correct Answer</label>
			<input type="text" name="meta_quiz3_correct" id="meta_quiz3_correct" value="<?php echo $quiz3_correct; ?>" />
		</div>
		<div class="field-wrapper">
			<label for="meta_quiz3_wrong1">Wrong Answer #1</label>
			<input type="text" name="meta_quiz3_wrong1" id="meta_quiz3_wrong1" value="<?php echo $quiz3_wrong1; ?>" />
		</div>
		<div class="field-wrapper">
			<label for="meta_quiz3_wrong2">Wrong Answer #2</label>
			<input type="text" name="meta_quiz3_wrong2" id="meta_quiz3_wrong2" value="<?php echo $quiz3_wrong2; ?>" />
		</div>
		<div class="field-wrapper">
			<label for="meta_quiz3_wrong3">Wrong Answer #3</label>
			<input type="text" name="meta_quiz3_wrong3" id="meta_quiz3_wrong3" value="<?php echo $quiz3_wrong3; ?>" />
		</div>
		
		<div class="spacer"></div>
		
		<div class="field-wrapper">
			<label for="meta_quiz4_question">Quiz Question #4</label>
			<textarea name="meta_quiz4_question" id="meta_quiz4_question"><?php echo $quiz4_question; ?></textarea>
		</div>
		<div class="field-wrapper">
			<label for="meta_quiz4_correct">Correct Answer</label>
			<input type="text" name="meta_quiz4_correct" id="meta_quiz4_correct" value="<?php echo $quiz4_correct; ?>" />
		</div>
		<div class="field-wrapper">
			<label for="meta_quiz4_wrong1">Wrong Answer #1</label>
			<input type="text" name="meta_quiz4_wrong1" id="meta_quiz4_wrong1" value="<?php echo $quiz4_wrong1; ?>" />
		</div>
		<div class="field-wrapper">
			<label for="meta_quiz4_wrong2">Wrong Answer #2</label>
			<input type="text" name="meta_quiz4_wrong2" id="meta_quiz4_wrong2" value="<?php echo $quiz4_wrong2; ?>" />
		</div>
		<div class="field-wrapper">
			<label for="meta_quiz4_wrong3">Wrong Answer #3</label>
			<input type="text" name="meta_quiz4_wrong3" id="meta_quiz4_wrong3" value="<?php echo $quiz4_wrong3; ?>" />
		</div>
		
		<div class="spacer"></div>
		
		<div class="field-wrapper">
			<label for="meta_quiz5_question">Quiz Question #5</label>
			<textarea name="meta_quiz5_question" id="meta_quiz5_question"><?php echo $quiz5_question; ?></textarea>
		</div>
		<div class="field-wrapper">
			<label for="meta_quiz5_correct">Correct Answer</label>
			<input type="text" name="meta_quiz5_correct" id="meta_quiz5_correct" value="<?php echo $quiz5_correct; ?>" />
		</div>
		<div class="field-wrapper">
			<label for="meta_quiz5_wrong1">Wrong Answer #1</label>
			<input type="text" name="meta_quiz5_wrong1" id="meta_quiz5_wrong1" value="<?php echo $quiz5_wrong1; ?>" />
		</div>
		<div class="field-wrapper">
			<label for="meta_quiz5_wrong2">Wrong Answer #2</label>
			<input type="text" name="meta_quiz5_wrong2" id="meta_quiz5_wrong2" value="<?php echo $quiz5_wrong2; ?>" />
		</div>
		<div class="field-wrapper">
			<label for="meta_quiz5_wrong3">Wrong Answer #3</label>
			<input type="text" name="meta_quiz5_wrong3" id="meta_quiz5_wrong3" value="<?php echo $quiz5_wrong3; ?>" />
		</div>
		
		
	</div>
		
	<?php        
}


add_action( 'add_meta_boxes', 'cd_meta_box_add' );
function cd_meta_box_add()
{
    add_meta_box( 'my-meta-box-id', 'Aditional Information', 'cd_meta_box_cb', 'post', 'normal', 'high' );
}


add_action( 'save_post', 'cd_meta_box_save' );
function cd_meta_box_save( $post_id )
{
    // Bail if we're doing an auto save
    if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
     
    // if our nonce isn't there, or we can't verify it, bail
    if( !isset( $_POST['meta_box_nonce'] ) || !wp_verify_nonce( $_POST['meta_box_nonce'], 'my_meta_box_nonce' ) ) return;
     
    // if our current user can't edit this post, bail
    if( !current_user_can( 'edit_post' ) ) return;
     
    // now we can actually save the data
    $allowed = array( 
        'a' => array( // on allow a tags
            'href' => array() // and those anchors can only have href attribute
        )
    );
     
    // Make sure your data is set before trying to save it
    if( isset( $_POST['meta_team_name'] ) )
        update_post_meta( $post_id, 'meta_team_name', esc_attr( $_POST['meta_team_name'] ) );
    
    if( isset( $_POST['meta_summary'] ) )
        update_post_meta( $post_id, 'meta_summary', esc_attr( $_POST['meta_summary'] ) );
    
    if( isset( $_POST['meta_topic'] ) )
        update_post_meta( $post_id, 'meta_topic', esc_attr( $_POST['meta_topic'] ) );
    
    if( isset( $_POST['meta_video'] ) )
        update_post_meta( $post_id, 'meta_video', $_POST['meta_video'] );
    
    if( isset( $_POST['meta_expert'] ) )
        update_post_meta( $post_id, 'meta_expert', $_POST['meta_expert'] );
    
    if( isset( $_POST['meta_call'] ) )
        update_post_meta( $post_id, 'meta_call', esc_attr( $_POST['meta_call'] ) );
    
    if( isset( $_POST['meta_blog'] ) )
        update_post_meta( $post_id, 'meta_blog', esc_attr( $_POST['meta_blog'] ) );
    
    $chk = isset( $_POST['meta_featured'] ) && $_POST['meta_featured'] ? 'on' : 'off';
    update_post_meta( $post_id, 'meta_featured', $chk );
    
    
    
    if( isset( $_POST['meta_quiz1_question'] ) )
        update_post_meta( $post_id, 'meta_quiz1_question', esc_attr( $_POST['meta_quiz1_question'] ) );
    if( isset( $_POST['meta_quiz1_correct'] ) )
        update_post_meta( $post_id, 'meta_quiz1_correct', esc_attr( $_POST['meta_quiz1_correct'] ) );
    if( isset( $_POST['meta_quiz1_wrong1'] ) )
        update_post_meta( $post_id, 'meta_quiz1_wrong1', esc_attr( $_POST['meta_quiz1_wrong1'] ) );
    if( isset( $_POST['meta_quiz1_wrong2'] ) )
        update_post_meta( $post_id, 'meta_quiz1_wrong2', esc_attr( $_POST['meta_quiz1_wrong2'] ) );
    if( isset( $_POST['meta_quiz1_wrong3'] ) )
        update_post_meta( $post_id, 'meta_quiz1_wrong3', esc_attr( $_POST['meta_quiz1_wrong3'] ) );
    
    if( isset( $_POST['meta_quiz2_question'] ) )
        update_post_meta( $post_id, 'meta_quiz2_question', esc_attr( $_POST['meta_quiz2_question'] ) );
    if( isset( $_POST['meta_quiz2_correct'] ) )
        update_post_meta( $post_id, 'meta_quiz2_correct', esc_attr( $_POST['meta_quiz2_correct'] ) );
    if( isset( $_POST['meta_quiz2_wrong1'] ) )
        update_post_meta( $post_id, 'meta_quiz2_wrong1', esc_attr( $_POST['meta_quiz2_wrong1'] ) );
    if( isset( $_POST['meta_quiz2_wrong2'] ) )
        update_post_meta( $post_id, 'meta_quiz2_wrong2', esc_attr( $_POST['meta_quiz2_wrong2'] ) );
    if( isset( $_POST['meta_quiz2_wrong3'] ) )
        update_post_meta( $post_id, 'meta_quiz2_wrong3', esc_attr( $_POST['meta_quiz2_wrong3'] ) );
    
    if( isset( $_POST['meta_quiz3_question'] ) )
        update_post_meta( $post_id, 'meta_quiz3_question', esc_attr( $_POST['meta_quiz3_question'] ) );
    if( isset( $_POST['meta_quiz3_correct'] ) )
        update_post_meta( $post_id, 'meta_quiz3_correct', esc_attr( $_POST['meta_quiz3_correct'] ) );
    if( isset( $_POST['meta_quiz3_wrong1'] ) )
        update_post_meta( $post_id, 'meta_quiz3_wrong1', esc_attr( $_POST['meta_quiz3_wrong1'] ) );
    if( isset( $_POST['meta_quiz3_wrong2'] ) )
        update_post_meta( $post_id, 'meta_quiz3_wrong2', esc_attr( $_POST['meta_quiz3_wrong2'] ) );
    if( isset( $_POST['meta_quiz3_wrong3'] ) )
        update_post_meta( $post_id, 'meta_quiz3_wrong3', esc_attr( $_POST['meta_quiz3_wrong3'] ) );
    
    if( isset( $_POST['meta_quiz4_question'] ) )
        update_post_meta( $post_id, 'meta_quiz4_question', esc_attr( $_POST['meta_quiz4_question'] ) );
    if( isset( $_POST['meta_quiz4_correct'] ) )
        update_post_meta( $post_id, 'meta_quiz4_correct', esc_attr( $_POST['meta_quiz4_correct'] ) );
    if( isset( $_POST['meta_quiz4_wrong1'] ) )
        update_post_meta( $post_id, 'meta_quiz4_wrong1', esc_attr( $_POST['meta_quiz4_wrong1'] ) );
    if( isset( $_POST['meta_quiz4_wrong2'] ) )
        update_post_meta( $post_id, 'meta_quiz4_wrong2', esc_attr( $_POST['meta_quiz4_wrong2'] ) );
    if( isset( $_POST['meta_quiz4_wrong3'] ) )
        update_post_meta( $post_id, 'meta_quiz4_wrong3', esc_attr( $_POST['meta_quiz4_wrong3'] ) );
    
    if( isset( $_POST['meta_quiz5_question'] ) )
        update_post_meta( $post_id, 'meta_quiz5_question', esc_attr( $_POST['meta_quiz5_question'] ) );
    if( isset( $_POST['meta_quiz5_correct'] ) )
        update_post_meta( $post_id, 'meta_quiz5_correct', esc_attr( $_POST['meta_quiz5_correct'] ) );
    if( isset( $_POST['meta_quiz5_wrong1'] ) )
        update_post_meta( $post_id, 'meta_quiz5_wrong1', esc_attr( $_POST['meta_quiz5_wrong1'] ) );
    if( isset( $_POST['meta_quiz5_wrong2'] ) )
        update_post_meta( $post_id, 'meta_quiz5_wrong2', esc_attr( $_POST['meta_quiz5_wrong2'] ) );
    if( isset( $_POST['meta_quiz5_wrong3'] ) )
        update_post_meta( $post_id, 'meta_quiz5_wrong3', esc_attr( $_POST['meta_quiz5_wrong3'] ) );
    
}












function sfc_get_DB($id) {
	$file = get_template_directory_uri() . '/db/sfc-' . $id . '.csv';
	
	if (($handle = fopen($file, "c")) !== FALSE) {
		$i = 0; 
		while (($lineData = fgetcsv($handle, 1000, ",")) !== FALSE) {
			for ($j=0; $j<count($lineData); $j++) { 
				$db[$i][$j] = $lineData[$j]; 
			} 
			$i++;
		}
		fclose($handle);
	}
	
	//sfc_print_array($db); die();
	return $db;
}


function sfc_write_DB($file_name, $data, $mode = 'w', $multi_array = 1) {
	$file = 'wp-content/themes/students-for-change.1.0/students-for-change/db/sfc-' . $file_name . '.csv';
	$fp = fopen($file, $mode);
	if($multi_array) {
		foreach ($data as $fields) {
	    	fputcsv($fp, $fields);
		}
	} else {
		fputcsv($fp, $data);
	}
	fclose($fp);
}


function sfc_check_post($part) {
	if(isset($_POST['cookie_name'])) {
		$name = esc_attr($_POST['cookie_name']);
		if(sfc_is_duplicate_name($name)) {
			return 11;
		} else {
			sfc_create_cookie($name);
			return 10;
		}
	}
	else return 0;
}


function sfc_create_cookie($name) {
	$cookie_name = 'sfc_score';
	$id = md5($name);
	$expire = time() + (60*60*24*356);
	$path = COOKIEPATH;
	
	setcookie($cookie_name, $id, $expire, $path);

	
	//id,name,score,badges,visits
	$list = array(array($id), array($name), array(0), array(0), array(1, time()));
	
	// write db to own db file
	sfc_write_DB($id, $list, 'c', 1);
	
	// add name to db of names
	sfc_write_DB('names', array($name, 0), 'a', 0);
}


function sfc_is_duplicate_name($name) {
	$DB = sfc_get_DB('names');
	
	foreach($DB as $rowKey => $row) {
		if($row[0] == $name) {
			return true;
		}
	}
	return false;
}


function sfc_add_view() {
	// get database and id
	$id = $_COOKIE['sfc_score'];
	$DB = sfc_get_DB($id);
	//sfc_print_array($DB);
	
	// get date last visited
	$last_visit = $DB[4][1];
	$one_day_ago = strtotime('-1 day');
	if($last_visit < $one_day_ago) {
		// more than 1 day
		
		// incrament number of visits
		$DB[4][0]++;
		
		// replace last visit date
		$DB[4][1] = time();
		
		// rewrite the db
		sfc_write_DB($id, $DB);
	}
}


function sfc_user_has_taken_quiz($post_id) {
	// get database and id
	$id = $_COOKIE['sfc_score'];
	$DB = sfc_get_DB($id);
	
	$quizzes = $DB[5];
	//sfc_print_array($quizzes); die();
	
	if(!empty($quizzes)) {
		foreach($quizzes as $key => $quiz_id) {
			if($quiz_id == $post_id)
				return true;
		}
	}
	
	return false;
}





function sfc_get_personal_progress() {
	return sfc_get_DB($_COOKIE['sfc_score']);
}


function sfc_get_leaderboard () {
	$DB = sfc_get_DB('names');
	
	// only get name => score
	foreach($DB as $rowKey => $row) {
		$arr[$row[0]] = $row[1];
	}
	
	// sort high -> low
	arsort($arr);
	
	// take top 10 values
	$leaderboard = array_slice($arr, 0, 10);
	//sfc_print_array($leaderboard); die();
	
	return $leaderboard;
}




function sfc_build_quiz_radio_buttons($radio_name, $correct, $wrong1, $wrong2, $wrong3) {
	$html[0] = '<input type="radio" name="' . $radio_name . '" value="1" id="' . $radio_name . '1" /><label for="' . $radio_name . '1">' . $correct . '</label><br/>';
	$html[1] = '<input type="radio" name="' . $radio_name . '" value="0" id="' . $radio_name . '2" /><label for="' . $radio_name . '2">' . $wrong1 . '</label><br/>';
	$html[2] = '<input type="radio" name="' . $radio_name . '" value="0" id="' . $radio_name . '3" /><label for="' . $radio_name . '3">' . $wrong2 . '</label><br/>';
	$html[3] = '<input type="radio" name="' . $radio_name . '" value="0" id="' . $radio_name . '4" /><label for="' . $radio_name . '4">' . $wrong3 . '</label><br/>';

	shuffle($html);
	return implode('', $html);
}



function sfc_check_quiz_post($post_id) {
	if( isset($_POST['quiz1']) && isset($_POST['quiz2']) && isset($_POST['quiz3']) && isset($_POST['quiz4']) && isset($_POST['quiz5']) ) {
		$sum = $_POST['quiz1'] + $_POST['quiz2'] + $_POST['quiz3'] + $_POST['quiz4'] + $_POST['quiz5'];
		if($sum == 5) $sum = 6;
		
		// get db and id
		$id = $_COOKIE['sfc_score'];
		$DB = sfc_get_DB($id);
		
		// add quiz sum to score
		$DB[2][0] = $DB[2][0] + $sum;
		
		// add post id to list of quizzes taken
		$DB[5][] = $post_id;
		
		// update user db
		sfc_write_DB($id, $DB);
		
		// update names db
		$DB_names = sfc_get_DB('names');
		foreach($DB_names as $rowKey => $row) {
			if($row[0] == $DB[1][0])
				$DB_names[$rowKey][1] = $DB[2][0];
		}
		sfc_write_DB('names', $DB_names);
		
		
		return $sum;
	} else return 0;
}







